﻿using CountryDepth.Library.BusinessLogic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CountryDepth.Web
{
    public partial class Users : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindUsersListBox();
                BindManagerDropDownList();
                if (lstUsers.SelectedItem != null)
                    BindUserData(Convert.ToInt32(lstUsers.SelectedItem.Value));
                else
                    ClearFromControls();
            }
        }

        protected void btnSaveUser_Click(object sender, ImageClickEventArgs e)
        {
            if (lstUsers.SelectedItem == null)
            {
                BLUser newUser = new BLUser();

                newUser.FullName = txtFullName.Text;
                newUser.UserName = txtUserName.Text;


                newUser.Password = txtPassword.Text;
                newUser.Telephone = txtTelephone.Text;
                newUser.Mobile = txtMobile.Text;
                newUser.Email = txtEmail.Text;
                newUser.Address = txtAddress.Text;

                newUser.Manager = new Item { Id = Convert.ToInt32(ddlManager.SelectedItem.Value), Value = ddlManager.SelectedItem.Text };

                newUser.InitialAnnualBalance = txtInitialAnnualBalance.Text == "" ? 0 : Convert.ToInt32(txtInitialAnnualBalance.Text);
                newUser.InitialCasualBalance = txtInitialCasualBalance.Text == "" ? 0 : Convert.ToInt32(txtInitialCasualBalance.Text);
                newUser.WorkingHoursPerDay = Convert.ToInt32(txtWorkingHoursPerDay.Text);

                newUser.IsFriWork = chkIsFriWork.Checked;
                newUser.IsSatWork = chkIsSatWork.Checked;
                newUser.IsSunWork = chkIsSunWork.Checked;
                newUser.IsMonWork = chkIsMonWork.Checked;
                newUser.IsTueWork = chkIsTueWork.Checked;
                newUser.IsWedWork = chkIsWedWork.Checked;
                newUser.IsThuWork = chkIsThuWork.Checked;

                if (newUser.Save())
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyScript", "alert('تم حفظ بيانات المستخدم بنجاح');", true);
                    BindUsersListBox();
                    BindManagerDropDownList();
                    ClearFromControls();
                }

            }
            else
            {
                BLUser existingUser = new BLUser();
                existingUser.UserId = Convert.ToInt32(lstUsers.SelectedItem.Value);

                existingUser.FullName = txtFullName.Text;
                existingUser.UserName = txtUserName.Text;


                existingUser.Password = txtPassword.Text;
                existingUser.Telephone = txtTelephone.Text;
                existingUser.Mobile = txtMobile.Text;
                existingUser.Email = txtEmail.Text;
                existingUser.Address = txtAddress.Text;

                existingUser.Manager = new Item { Id = Convert.ToInt32(ddlManager.SelectedItem.Value), Value = ddlManager.SelectedItem.Text };

                existingUser.InitialAnnualBalance = txtInitialAnnualBalance.Text == "" ? 0 : Convert.ToInt32(txtInitialAnnualBalance.Text);
                existingUser.InitialCasualBalance = txtInitialCasualBalance.Text == "" ? 0 : Convert.ToInt32(txtInitialCasualBalance.Text);
                existingUser.WorkingHoursPerDay = Convert.ToInt32(txtWorkingHoursPerDay.Text);

                existingUser.IsFriWork = chkIsFriWork.Checked;
                existingUser.IsSatWork = chkIsSatWork.Checked;
                existingUser.IsSunWork = chkIsSunWork.Checked;
                existingUser.IsMonWork = chkIsMonWork.Checked;
                existingUser.IsTueWork = chkIsTueWork.Checked;
                existingUser.IsWedWork = chkIsWedWork.Checked;
                existingUser.IsThuWork = chkIsThuWork.Checked;

                if (existingUser.Save())
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyScript", "alert('تم تعديل بيانات المستخدم بنجاح');", true);
                    BindUsersListBox();
                    BindManagerDropDownList();
                    ClearFromControls();
                    //lstUsers.SelectedItem.Selected = false;
                }
            }

        }

        protected void btnNewUser_Click(object sender, ImageClickEventArgs e)
        {
            ClearFromControls();

            if (lstUsers.SelectedItem != null)
                lstUsers.SelectedItem.Selected = false;
        }

        protected void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindManagerDropDownList();
            BindUserData(Convert.ToInt32(lstUsers.SelectedItem.Value));
        }

        #region private Methods

        private void BindUsersListBox()
        {
            try
            {
                BLUser blUser = new BLUser();
                List<Item> users = blUser.GetAllUsers();
                if (users != null)
                {
                    lstUsers.DataSource = users;
                    lstUsers.DataTextField = "Value";
                    lstUsers.DataValueField = "Id";

                    lstUsers.DataBind();
                }
                else
                {
                    lstUsers.DataSource = null;
                    lstUsers.DataBind();
                }
            }
            catch (Exception exc)
            {

            }
        }

        private void BindManagerDropDownList()
        {
            try
            {
                BLUser blUser = new BLUser();
                List<Item> users = blUser.GetAllUsers();
                if (users != null)
                {
                    Item empty = new Item { Id = 0, Value = "" };
                    users.Insert(0, empty);
                    ddlManager.DataSource = users;
                    ddlManager.DataTextField = "Value";
                    ddlManager.DataValueField = "Id";

                    ddlManager.DataBind();

                    foreach (ListItem li in ddlManager.Items)
                        li.Enabled = true;

                    if (lstUsers.SelectedItem != null)
                    {
                        ddlManager.Items.FindByValue(lstUsers.SelectedItem.Value).Enabled = false;
                    }
                }
                else
                {
                    lstUsers.DataSource = null;
                    lstUsers.DataBind();
                }
            }
            catch (Exception exc)
            {

            }
        }

        private void ClearFromControls()
        {
            txtFullName.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtTelephone.Text = "";
            txtMobile.Text = "";
            txtEmail.Text = "";
            txtAddress.Text = "";
            txtInitialAnnualBalance.Text = "";
            txtInitialCasualBalance.Text = "";
            txtWorkingHoursPerDay.Text = "";
            chkIsFriWork.Checked = false;
            chkIsSatWork.Checked = false;
            chkIsSunWork.Checked = false;
            chkIsMonWork.Checked = false;
            chkIsTueWork.Checked = false;
            chkIsWedWork.Checked = false;
            chkIsThuWork.Checked = false;

            if (ddlManager.SelectedItem != null)
                ddlManager.SelectedItem.Selected = false;
        }

        private void BindUserData(int userId)
        {
            ClearFromControls();
            BLUser currentUser = new BLUser(userId);
            if (currentUser != null)
            {
                txtFullName.Text = currentUser.FullName;
                txtUserName.Text = currentUser.UserName;
                txtPassword.Text = currentUser.Password;
                txtTelephone.Text = currentUser.Telephone;
                txtMobile.Text = currentUser.Mobile;
                txtEmail.Text = currentUser.Email;
                txtAddress.Text = currentUser.Address;
                txtInitialAnnualBalance.Text = currentUser.InitialAnnualBalance.ToString();
                txtInitialCasualBalance.Text = currentUser.InitialCasualBalance.ToString();
                txtWorkingHoursPerDay.Text = currentUser.WorkingHoursPerDay.ToString();

                if (ddlManager.SelectedItem != null)
                    ddlManager.SelectedItem.Selected = false;

                if (currentUser.Manager.Id != 0)
                    ddlManager.Items.FindByValue(currentUser.Manager.Id.ToString()).Selected = true;

                chkIsFriWork.Checked = currentUser.IsFriWork;
                chkIsSatWork.Checked = currentUser.IsSatWork;
                chkIsSunWork.Checked = currentUser.IsSunWork;
                chkIsMonWork.Checked = currentUser.IsMonWork;
                chkIsTueWork.Checked = currentUser.IsTueWork;
                chkIsWedWork.Checked = currentUser.IsWedWork;
                chkIsThuWork.Checked = currentUser.IsThuWork;
            }
        }

        #endregion
    }
}